package basic_datastructure.tree;

import java.util.Arrays;
import java.util.LinkedList;

public class RecurPreInPostTraversal {
    public static void main(String[] args) {
        LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(new Integer[]{
                1,2,4,null,null,5,null,null,3,6,null,null,7,null,null
        }));
        TreeNode binaryTree = createBinaryTree(inputList);
        System.out.println("前序遍历：");
        preOrderTraversal(binaryTree);
        System.out.println();
        System.out.println("中序遍历：");
        inOrderTraversal(binaryTree);
        System.out.println();
        System.out.println("后序遍历：");
        postOrderTraversal(binaryTree);
    }
    public static TreeNode createBinaryTree(LinkedList<Integer> inputList){
        if(inputList == null || inputList.isEmpty()){
            return null;
        }
        TreeNode node = null;
        Integer data = inputList.removeFirst();
        if(data != null){
            node = new TreeNode(data);
            node.leftChild = createBinaryTree(inputList);
            node.rightChild = createBinaryTree(inputList);
        }
        return node;
    }
    public static void preOrderTraversal(TreeNode node){
        if(node == null){
            return;
        }
        System.out.print(node.data+" ");
        preOrderTraversal(node.leftChild);
        preOrderTraversal(node.rightChild);
    }
    public static void inOrderTraversal(TreeNode node){
        if(node == null){
            return;
        }
        inOrderTraversal(node.leftChild);
        System.out.print(node.data+" ");
        inOrderTraversal(node.rightChild);
    }
    public static void postOrderTraversal(TreeNode node){
        if(node == null){
            return;
        }
        postOrderTraversal(node.leftChild);
        postOrderTraversal(node.rightChild);
        System.out.print(node.data+" ");
    }
}
